package com.jxk.consumer.controller;

import com.jxk.common.entity.User;
import com.jxk.consumer.fein.UserRemoteClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class HelloServiceController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    private UserRemoteClient userRemoteClient;
    @GetMapping("/article/callHello2")
    public String callHello2() {
        return userRemoteClient.hello();
    }
    @GetMapping("/callHello/{id}")
    public User callHello(@PathVariable("id")Integer id) {
        //return restTemplate.getForObject("http://localhost:8083/house/hello",String.class);
        //String result = restTemplate.getForObject("http://eureka-client-user-service/user/hello",String.class);
        log.info("我是调用者");
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        System.out.println("用户ID:" + request.getHeader("uid"));
        User user = userRemoteClient.selectOne(id);
        System.out.println("调用结果：" + user);
        return user;
    }
}
